# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=kopano-webapp-files
pkgdesc="files plugin for kopano-webapp"
pkgver=3.0.1
pkgrel=2
# ppc64le and riscv64 blocked by libmdbx -> kopano-core -> kopano-webapp
arch="noarch !ppc64le !riscv64"
url="https://stash.kopano.io/projects/KWA/repos/files/browse"
license="AGPL-3.0-only"
options="!check" # No test suite
depends="kopano-webapp"
# kopano-core necessary for mapi php module
makedepends="apache-ant openjdk8 kopano-webapp-src kopano-core"
source="
	kopano-webapp-files-$pkgver.zip::https://stash.kopano.io/rest/api/latest/projects/KWA/repos/files/archive?at=refs%2Ftags%2Fv$pkgver&format=zip
	kopano-webapp-files.ini
"

_pluginname="${pkgname//kopano-webapp-/}"

builddir="$srcdir/"

#helper function
unpack() {
	local u
	verify
	initdcheck
	mkdir -p "$srcdir"
	local gunzip="$(command -v pigz || echo gunzip)"
	[ $gunzip = "/usr/bin/pigz" ] && gunzip="$gunzip -d"
	for u in $source; do
		local s
		local filename="$(filename_from_uri $u)"
		local new_root_dir="$builddir/${filename%%-[0-9]*}"
		if is_remote "$u"; then
			s="$SRCDEST/$filename"
		else
			s="$startdir/$u"
		fi
		case "$s" in
			*.tar)
				msg "Unpacking $s..."
				mkdir -p "$new_root_dir"
				tar -C "$new_root_dir" -xf "$s" ;;
			*.tar.gz|*.tgz)
				msg "Unpacking $s..."
				mkdir -p "$new_root_dir"
				$gunzip -c "$s" | tar -C "$new_root_dir" -x ;;
			*.tar.bz2)
				msg "Unpacking $s..."
				tar -C "$new_root_dir" -jxf "$s" ;;
			*.tar.lz)
				msg "Unpacking $s..."
				tar -C "$new_root_dir" --lzip -xf "$s" ;;
			*.tar.lzma)
				msg "Unpacking $s..."
				unlzma -T 0 -c "$s" | tar -C "$new_root_dir" -x  ;;
			*.tar.xz)
				msg "Unpacking $s..."
				local threads_opt
				if [ "$(readlink -f "$(command -v unxz)")" != "/bin/busybox" ]; then
					threads_opt="--threads=0"
				fi
				unxz $threads_opt -c "$s" | tar -C "$new_root_dir" -x ;;
			*.zip)
				msg "Unpacking $s..."
				unzip -n -q "$s" -d "$new_root_dir" ;;
		esac
	done
}

prepare() {
	cp -R /usr/share/src/kopano-webapp/ kopano-webapp
	find "kopano-webapp/plugins/" -type d -mindepth 1 -maxdepth 2 -print0 | xargs -0 -- rm -rf
	mv $pkgname kopano-webapp/plugins/$_pluginname
	default_prepare
}

build() {
	cd kopano-webapp
	ant tools
	cd plugins/$_pluginname
	ant deploy -Droot-folder="$(pwd)/../../" -Dtarget-folder="$(pwd)/../../deploy/plugins"
}

package() {
	cd "$srcdir/kopano-webapp/deploy/plugins/$_pluginname/"

	sed -i -e "s|\(PLUGIN_FILESBROWSER_LOGLEVEL', \)\(.*\)\();$\)|\1'ERROR'\3|" config.php
	sed -i -e "s|\(PLUGIN_FILES_CACHE_DIR', \)\(.*\)\();$\)|\1'/var/lib/kopano-webapp/plugins/files'\3|" config.php

	# /usr/share
	mkdir -p "$pkgdir/usr/share/webapps/kopano-webapp/plugins/$_pluginname/"
	cp -R -- * "$pkgdir/usr/share/webapps/kopano-webapp/plugins/$_pluginname/"
	rm -f "$pkgdir/usr/share/webapps/kopano-webapp/plugins/$_pluginname/config.php"

	# /var/lib
	install -dm 0700 -o kopano-webapp -g root "$pkgdir/var/lib/kopano-webapp/plugins/$_pluginname"

	# /etc
	if [ -e "config.php" ]; then
		## perform settings
		# convert windows line break to unix: http://stackoverflow.com/questions/11680815/removing-windows-newlines-on-linux-sed-vs-awk
		sed -i -e $'s/\r//' config.php
		install -dm 0750 -g kopano-webapp "$pkgdir/etc/webapps/kopano-webapp/plugins/$_pluginname/"
		## config mains
		install -m 0750 -g kopano-webapp config.php "$pkgdir/etc/webapps/kopano-webapp/plugins/$_pluginname/config.php"
		ln -sf "/etc/webapps/kopano-webapp/plugins/$_pluginname/config.php" "$pkgdir/usr/share/webapps/kopano-webapp/plugins/$_pluginname/config.php"
		## config examples
		install -m 0750 "$pkgdir/etc/webapps/kopano-webapp/plugins/$_pluginname/config.php" "$pkgdir/etc/webapps/kopano-webapp/plugins/$_pluginname/config.example.php"
	fi

	mkdir -p "$pkgdir/etc/php/conf.d"
	cp "$srcdir/$pkgname.ini" "$pkgdir/etc/php/conf.d"
}
sha512sums="
7cd4ad26833920397c4d864077e29f24e34848c086a120e85fd838cffc8326ced19fd8b9c3ccd182e3b64c8f18db363e3a172f00d5adecb24141928fe3e303bd  kopano-webapp-files-3.0.1.zip
8bda45524ba341753a1503e5a8bf2b4f73a365d9cf9b71c0e33cdb1f3b2a32b39186bad39fedc4a955d724884dee6333ceb218a44859151cb1964932c4fab198  kopano-webapp-files.ini
"
